package com.woniu.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.commons.exception.YogaException;
import com.woniu.commons.model.Result;
import com.woniu.commons.util.JwtUtil;
import com.woniu.commons.util.RedisUtil;
import com.woniu.model.form.PageVenueBaseApplyForm;
import com.woniu.model.form.PageVenueBaseForm;
import com.woniu.model.form.UpdateVenueBaseLicenseForm;
import com.woniu.model.form.addVenueBaseTimeForm;
import com.woniu.model.model.YVenueBase;
import com.woniu.model.param.AddQualificationParam;
import com.woniu.model.param.PageQualificationByVenueIdParam;
import com.woniu.model.param.UpdateVenueBaseLicenseParam;
import com.woniu.model.param.addVenueBaseTimeParam;
import com.woniu.service.YVenueBaseService;
import com.woniu.util.FileUtil;
import com.woniu.util.OSSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author EdisonLiu
 * @since 2024年05月23日
 */
@RestController
@RequestMapping("/yVenueBase")
@Api(tags = "场馆详细接口")
public class YVenueBaseController {
    @Resource
    private YVenueBaseService venueBaseService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 根据场馆id查询场馆base 详细信息
     * @param
     * @return
     */
    @GetMapping("/loadVenueBaseById")
    @ApiOperation(value = "根据场馆id查询场馆base（详细）")
    public Result<?> loadVenueBaseById(@RequestHeader String token){
        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("venueId").toString());

        YVenueBase venueBase = venueBaseService.loadVenueBaseById(venueId);

        return Result.newInstance(YVenueBase.class).setData(venueBase).setMsg("查询成功").setCode(200);
    }



    /**
     * 根据场馆id查一个详细信息对象回
     * @param
     * @return
     */
    @GetMapping("/venueIdByVenueBase/{id}")
    @ApiOperation(value = "根据场馆id查一个详细信息对象回")
    public Result<YVenueBase> venueIdByVenueBase(@PathVariable("id") Integer venueId){
        YVenueBase venueBase = venueBaseService.getById(venueId);
        return Result.newInstance(YVenueBase.class).setData(venueBase).setMsg("查询成功").setCode(200);
    }






    /**
     * 根据id查找场馆信息
     * @param id
     * @return
     */
    @GetMapping("/byId/{id}")
    @ApiOperation("根据id查找场馆信息")
    public Result<YVenueBase> venueById(@PathVariable("id") Integer id)throws YogaException {
        if (id==null)throw new YogaException(500,"参数异常");
        YVenueBase venueBase = venueBaseService.getById(id);
        return Result.newInstance(YVenueBase.class).setData(venueBase).setMsg("查询成功").setCode(200);
    }

    /**
     * 更新场馆信息（除了照片）
     * @param venueBase 场馆更改后的信息
     * @return
     */
    @PostMapping("/updateVenueBaseById")
    @ApiOperation(value = "根据场馆id更新场馆base（详细）(除了场馆照片)")
    public Result<?> updateVenueBaseById(@RequestBody YVenueBase venueBase){
        Integer i = venueBaseService.updateVenueBaseById(venueBase);
        if (!i.equals(1)){
            return Result.getResult().setCode(500).setMsg("更新失败");
        }
        return Result.getResult().setMsg("更新成功");
    }

    /**
     * 根据场馆id更新场馆base
     * @param form
     * @param token
     * @return
     */
    @PostMapping("/addVenueBaseTime")
    @ApiOperation(value = "根据场馆id更新场馆base（详细）")
    public Result<?> addVenueBaseTime(@RequestBody addVenueBaseTimeForm form, @RequestHeader String token){
        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("venueId").toString());

        addVenueBaseTimeParam param = BeanUtil.toBean(form, addVenueBaseTimeParam.class);
        param.setVenueId(venueId);

        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
        param.setBusinessStartTime(LocalDateTime.parse(form.getBusinessStartTime(), formatter).toString());
        param.setBusinessEndTime(LocalDateTime.parse(form.getBusinessEndTime(), formatter).toString());
        param.setCreateTime(LocalDateTime.parse(form.getCreateTime(), formatter).toString());

        Integer i = venueBaseService.addVenueBaseTime(param);

        if (!i.equals(1)){
            return Result.getResult().setCode(500).setMsg("更新失败");
        }
        return Result.getResult().setMsg("更新成功");
    }




    /**
     * 根据场馆id更新场馆许可证
     * @param form 场馆许可的multipartfile和id
     * @return
     */
    @PostMapping("/updateVenueBaseLicenseById")
    @ApiOperation(value = "根据场馆id更新场馆许可证")
    public Result<?> updateVenueBaseLicenseById( UpdateVenueBaseLicenseForm form) throws Exception {
        if (!FileUtil.isImageFile(form.getVenueLicenseFile().getInputStream())) {
            throw new Exception("文件类型有误，请检查");
        }

        //证书
        //获取上传文件的原始文件名
        String oldname = form.getVenueLicenseFile().getOriginalFilename();

        //获取上传文件的后缀名
        String substring = oldname.substring(oldname.lastIndexOf("."));

        //生成一个雪花作为文件名
        String idStr = new Snowflake(1, 1).nextIdStr();

        //调用OSSUtil工具类的方法，将文件上传到阿里云
        String file = OSSUtil.uploadFile(form.getVenueLicenseFile().getInputStream(), idStr + substring, "edison-coco");
        UpdateVenueBaseLicenseParam param = BeanUtil.toBean(form, UpdateVenueBaseLicenseParam.class);
        param.setVenueLicense(file);

        Integer i = venueBaseService.updateVenueBaseLicenseById(param);
        if (!i.equals(1)){
            return Result.getResult().setCode(500).setMsg("更新失败");
        }
        return Result.getResult().setMsg("更新成功");
    }

    /**
     * 分页显示场馆信息
     * @param form 分页显示场馆信息表单
     * @return
     */
    @PostMapping("/pageVenueBase")
    @ApiOperation(value = "分页显示场馆信息")
    public Result<?> pageVenueBase(@RequestBody PageVenueBaseForm form){
        Page<YVenueBase> page = venueBaseService.pageVenueBase(form);
        return Result.newInstance(Page.class).setMsg("载入成功").setData(page);
    }

    /**
     * 分页显示教练可入驻的场馆信息
     * @param form 分页显示场馆信息表单
     * @return
     */
    @PostMapping("/pageVenueBaseApply")
    @ApiOperation(value = "分页显示教练可入驻的场馆信息")
    public Result<?> pageVenueBaseApply(@RequestBody PageVenueBaseApplyForm form, @RequestHeader String token){
        String refreshToken = redisUtil.get(token);
        Integer coachId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());
        form.setCoachId(coachId);

        Page<YVenueBase> page = venueBaseService.pageVenueBaseApply(form);
        return Result.newInstance(Page.class).setMsg("载入成功").setData(page);
    }
}

